var ejzhrj = {
    setBtnStatus: function () {
        var row = $('#ejzhrj_grid').datagrid('getSelected');
        if (row) {   //被选择的应付信用证
            $("#btn_ejzhrj_delete").show();
            $("#btn_ejzhrj_rj").show();
        } else {  //未被选择的应付票据
            $("#btn_ejzhrj_delete").show();
            $("#btn_ejzhrj_rj").show();
        }
    }
}

$(function () {
    //======================
    //======设置查询条件========
    //======================
    //内部公司
    $("#ejzhrj_toolbar [name='gsId']").gsjgtree(1);
    //起始时间
    $("#ejzhrj_toolbar [name='endDate']").datebox();
    //客户加载
    $("[name='khId']").qyjgzcombobox();
    //搜索按钮
    $("#ejzhrj_toolbar [name='searchButton']").click(function () {
        $(grid).datagrid({
            queryParams: {
                endDate: $("#ejzhrj_toolbar [name='endDate']").val(),
                gsId: $("#ejzhrj_toolbar [name='gsId']").val(),
                khId: $("#ejzhrj_toolbar [name='khId']").val()
            }});
    });

    //
    $("#btn_ejzhrj_delete").click(function () {
        ejzhrj_del('删除日结', 'ejzhrj_del');
    });

    $("#ejzh_rjExport").click(function () {
            var gsId =    $("#gsid").combotree("getValue");
            var endDate =  $("#ejzhrj_toolbar [name='endDate']").val();
            var   khId = $("#ejzhrj_toolbar [name='khId']").val();
            var url = $.webPath() + "/report/ejzhrj/ejzhrjExport?endDate="+endDate;
            if(gsId){
              url += "&&gsId="+gsId;
            }
            if(khId){
                url += "&&khId="+khId;
             }
            window.open(url);
    });


//
    $("#btn_ejzhrj_rj").click(function () {
        ejzhrj_rj('日结', 'ejzhrj_rj');
    });

//======================
//========统计列表========
//======================
    var yhckcolumns = [
        [
            {field: 'yearDate', title: '年|月|日', width: 100, align: 'center'},
            {field: 'zh', title: '二级账户', width: 100, align: 'center'},
            {field: 'ejzhmc', title: '二级账户名称', width: 100, align: 'center'},
            {field: 'gsmc', title: '园区', width: 100, align: 'center'},
            {field: 'khmc', title: '客户名称', width: 100, align: 'center'},
            {field: "zhye", title: "账户余额", width: 70, align: 'right', formatter: function (value, row, index) {
                return '<a href="#" class="ye-linkbutton" style="color:red" ejzhid="' + row["ejzhid"] + '" >' + formatJe2(value, ",", ".") + '</a>';
            }},
            {field: "dfje", title: "贷方金额", width: 70, align: 'right'},
            {field: "jfje", title: "借方金额", width: 70, align: 'right'},
            {field: "bzj", title: "保证金", width: 70, align: 'right',
                formatter: function (value) {
                    var sl = formatJe2(value, ",");
                    return sl;
                }},
            {field: "zhze", title: "账户总额", width: 70, align: 'right',
                formatter: function (value) {
                    var sl = formatJe2(value, ",");
                    return sl;
                }}
        ]
    ];

    var grid = $('#ejzhrj_grid').datagrid({
        url: $.webPath() + '/report/ejzhrj/ejzhrjList',
        toolbar: "#ejzhrj_toolbar",
        rownumbers: true,
        pagination: true,
        singleSelect: true,
        fit: true,
        fitColumns: true,
        queryParams: {
            endDate: $("#ejzhrj_toolbar [name='endDate']").val()
        },
        columns: yhckcolumns,
        onLoadSuccess: function () {
            $(".ye-linkbutton").linkbutton({plain: true}).unbind("click").bind("click", function () {
                $.window({
                    title: '二级账户往来明细',
                    width: 1080,
                    height: 600,
                    cache: false,
                    maximizable: true,
                    url: $.webPath() + '/report/wlz/ejzhmx_detail?ejzhId=' + $(this).attr("ejzhid"),
                    modal: true,
                    buttons: [],
                    onMaximize: function () {
                        $(this).find("#ejzhmx_grid").edatagrid("resize");
                    },
                    onRestore: function () {
                        $(this).find("#ejzhmx_grid").edatagrid("resize");
                    }
                });
            });
            ejzhrj.setBtnStatus();
        },
        onSelect: function (rowIndex, rowData) {
            ejzhrj.setBtnStatus();
        },
        onCheck: function () {
            ejzhrj.setBtnStatus();
        }
    });
});


function ejzhrj_rj(title, action) {
    var gsId = $("#gsid").combotree("getValue");
    if (!gsId) {
        $.messager.alert("提示", "请选择园区!", "warning");
        return;
    }
    var href = $.webPath() + "/report/ejzhrj/to_ejzhrj_rj?gsId=" + gsId;
    var actionUrl = $.webPath() + "/report/ejzhrj/ejzhrj_rj";
    ejzhrj_rjDialog(title, href, actionUrl);
}

function ejzhrj_del(title, action) {
    var gsId = $("#gsid").combotree("getValue");
    if (!gsId) {
        $.messager.alert("提示", "请选择园区!", "warning");
        return;
    }
    var href = $.webPath() + "/report/ejzhrj/to_ejzhrj_delete?gsId=" + gsId;
    var actionUrl = $.webPath() + "/report/ejzhrj/ejzhrj_delete";
    ejzhrj_delDialog(title, href, actionUrl);
}


function ejzhrj_rjDialog(title, href, actionUrl) {
    var win = $.window({
        title: title,
        width: 350,
        height: 200,
        cache: false,
        url: href,
        modal: true,
        buttons: [
            {
                text: '确定',
                handler: function () {
                    var rq = $(win).find("#form_ejzhrj_rj [name='ejzhrjrq']").val();
                    var gsmc = $(win).find("#form_ejzhrj_rj [name='gsmc']").val();
                    $.messager.confirm('提示', '是否对【' + gsmc + '】【' + rq + '】二级账户进行资金日结?', function (r) {
                        if (r) {
                            $(win).mask();
                            $('#form_ejzhrj_rj').form('submit', {
                                url: actionUrl,
                                onSubmit: function () {
                                    var isValid = $(this).form('validate');
                                    if (!isValid) {
                                        $(win).mask('hide');
                                        return false;
                                    }
                                    return isValid;
                                },
                                success: function (data) {
                                    $(win).mask('hide');
                                    var json = $.parseJSON(data);
                                    if (json.statusCode == 200) {
                                        $(win).window('close');
                                        $('#ejzhrj_grid').datagrid('reload');
                                        $.messager.show({
                                            title: '提示',
                                            msg: json.message,
                                            timeout: 3000,
                                            showType: 'slide'
                                        });
                                    } else
                                        $.messager.alert('提示', json.message, 'error');
                                }
                            });
                        }
                    });
                }
            }
        ]
    });
}


function ejzhrj_delDialog(title, href, actionUrl) {
    var win = $.window({
        title: title,
        width: 350,
        height: 200,
        cache: false,
        url: href,
        modal: true,
        buttons: [
            {
                text: '确定',
                handler: function () {
                    var rq = $(win).find("#form_ejzhrj_delete [name='ejzhscrq']").val();
                    var gsmc = $(win).find("#form_ejzhrj_delete [name='gsmc']").val();
                    $.messager.confirm('提示', '是否对【' + gsmc + '】【' + rq + '】二级账户资金日结进行删除?', function (r) {
                        if (r) {
                            $(win).mask();
                            $('#form_ejzhrj_delete').form('submit', {
                                url: actionUrl,
                                onSubmit: function () {
                                    var isValid = $(this).form('validate');
                                    if (!isValid) {
                                        $(win).mask('hide');
                                        return false;
                                    }
                                    return isValid;
                                },
                                success: function (data) {
                                    $(win).mask('hide');
                                    var json = $.parseJSON(data);
                                    if (json.statusCode == 200) {
                                        $(win).window('close');
                                        $('#ejzhrj_grid').datagrid('reload');
                                        $.messager.show({
                                            title: '提示',
                                            msg: json.message,
                                            timeout: 3000,
                                            showType: 'slide'
                                        });
                                    } else
                                        $.messager.alert('提示', json.message, 'error');
                                }
                            });
                        }
                    });
                }
            }
        ]
    });
}
